package com.ghn.cocknovel.ui.activity.main.vm

import android.app.Application
import android.widget.TextView
import androidx.lifecycle.MutableLiveData
import com.example.basemodel.base.BaseViewModel
import com.ghn.cocknovel.net.DataService
import com.kt.NetworkModel.bean.*
import com.kt.NetworkModel.net.CallBack
import com.kt.NetworkModel.net.RxUtils

/**
 * @author 浩楠
 *
 * @date 2023/10/9-16:24.
 *
 *      _              _           _     _   ____  _             _ _
 *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
 *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
 *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
 *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
 * @Description: TODO
 */
open class MainViewModel(application: Application) : BaseViewModel(application) {
    companion object {
        val TAG: String? = MainViewModel::class.simpleName
    }

    val message = MutableLiveData<QuickMessageBean?>()
    val onlinetime = MutableLiveData<OnlineBean?>()
    val cumulative = MutableLiveData<CumulativeBean?>()
    val lastInformation = MutableLiveData<InformationBean?>()
    val currentInformation = MutableLiveData<InformationBean?>()
    var upUserLogoUrlSuccess = MutableLiveData<String>()
    var upUserLogoSuccess = MutableLiveData<Boolean>()
    var queryTransitSeatStatisticsBeanLiveData = MutableLiveData<QueryTransitSeatStatisticsBean>()
    var queryDriverOrderStatisticsByTaskBeanLiveData = MutableLiveData<QueryDriverOrderStatisticsByTaskBean>()

    /**
     * 获取司机的个人信息接口
     */
    open fun getmessage() {
        DataService.quickMessage().compose(RxUtils.mainSync())
            .subscribe(object : CallBack<QuickMessageBean>() {
                override fun onResult(result: QuickMessageBean?) {
                    result?.also {
                        message.value = it
                    }
                }
            })
    }

    /**
     *  在线时常
     */
    open fun getonlinetime() {
        DataService.getonlinetime().compose(RxUtils.mainSync())
            .subscribe(object : CallBack<OnlineBean>() {
                override fun onResult(result: OnlineBean?) {
                    result?.also {
                        onlinetime.value = it
                    }
                }
            })
    }

    /**
     *  累计订单数统计
     */
    open fun getcumulative() {
        DataService.getcumulative().compose(RxUtils.mainSync())
            .subscribe(object : CallBack<CumulativeBean>() {
                override fun onResult(result: CumulativeBean?) {
                    result?.also {
                        cumulative.value = it
                    }
                }
            })
    }

    /**
     *  查询司机当前任务信息
     */
    open fun getinformation() {
        DataService.getinformation().compose(RxUtils.mainSync())
            .subscribe(object : CallBack<InformationBean>() {
                override fun onResult(result: InformationBean?) {
                    currentInformation.value = result
                    if (result != null && result.taskState == 3) {

                    } else {
                        getaboutinformation()
                    }

                }
            })
    }

    /**
     *  查询上一次任务信息
     */
    open fun getaboutinformation() {
        DataService.getaboutinformation().compose(RxUtils.mainSync())
            .subscribe(object : CallBack<InformationBean>() {
                override fun onResult(result: InformationBean?) {
                    result?.also {
                        lastInformation.value = it
                    }
                }
            })
    }

    /**
     *  车辆人数统计
     */
    fun queryTransitSeatStatistics(taskId: String) {
        DataService.queryTransitSeatStatistics(taskId).compose(RxUtils.mainSync())
            .subscribe(object : CallBack<QueryTransitSeatStatisticsBean>() {
                override fun onResult(result: QueryTransitSeatStatisticsBean?) {
                    result?.also {
                        queryTransitSeatStatisticsBeanLiveData.value = result
                    }
                }
            })
    }

    /**
     *  根据任务id 查询订单量统计
     */
    fun queryDriverOrderStatisticsByTaskBean(taskId: String) {
        DataService.queryDriverOrderStatisticsByTask(taskId).compose(RxUtils.mainSync())
            .subscribe(object : CallBack<QueryDriverOrderStatisticsByTaskBean>() {
                override fun onResult(result: QueryDriverOrderStatisticsByTaskBean?) {
                    result?.also {
                        queryDriverOrderStatisticsByTaskBeanLiveData.value = it
                    }
                }
            })
    }

    fun uploadFileNet(path: String, fileType: String = "siteImage") {
        launch(
            block = {
                upUserLogoUrlSuccess.postValue(DataService.uploadFile(path, fileType).string())
            }
        )
    }

    fun driverUpdateTransitDriverInfo(driverAvatar: String) {
        launch(
            block = {
                val driverUpdateTransitDriverInfo = DataService.driverUpdateTransitDriverInfo(driverAvatar)
                upUserLogoSuccess.postValue(driverUpdateTransitDriverInfo.isSuccess())
            }
        )
    }

}